package com.zhongan.ribbon.demo;

import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerStats;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.client.http.RestClient;

@SuppressWarnings("deprecation")
public class RibbonDemo2 {

	public static void main(String[] args) throws Exception {
		ConfigurationManager.loadPropertiesFromResources("sample-client.properties");
		System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));
		@SuppressWarnings("deprecation")
		RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
		HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
		// 获取负载均衡结果
		HttpResponse response = client.executeWithLoadBalancer(request);
		System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());
		

		for (int i = 0; i < 10; i++) {
			
		}

		/*@SuppressWarnings("rawtypes")
		ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
		Map<Server, ServerStats> serverStatsMap = lb.getLoadBalancerStats().getServerStats();
		Set<Entry<Server, ServerStats>> entrys = serverStatsMap.entrySet();
		for (Entry<Server, ServerStats> entry : entrys) {
			Server server = entry.getKey();
			ServerStats serverStates = entry.getValue();
			System.out.println(server.getHost() + "," + server.getHostPort() + "," + server.getPort() + "," + server.getZone() + "," + server.getMetaInfo().getAppName()
					+ "," + serverStates.getTotalRequestsCount());
		}

		ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", "www.zhongan.com:80,www.baidu.com:80,www.linkedin.com:80");
		
		System.out.println("changing servers ...");
		Thread.sleep(3000);*/

		/*for (int i = 0; i < 20; i++) {
			HttpResponse response = client.executeWithLoadBalancer(request);
			System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());
		}*/
		// System.out.println(lb.getLoadBalancerStats());
	}

}
